package com.tmpt.utils.report;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author Helen
 * @version 1.0
 * @date 2020/1/15 21:26
 * @Description:
 */
public class TestPdf2 {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("application/pdf");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            reader = new PdfReader(""); // 模版文件目录
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            form.setField("CUSTOMERNAME", "as该多好公司");
            form.setField("TEL", "123456asdzxc");
            form.setField("CONTACT", "我是联系人123");

            stamper.setFormFlattening(true); // 这句不能少
            stamper.close();
            reader.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }

        ServletOutputStream sos = resp.getOutputStream();
        bos.writeTo(sos);
        sos.flush();
        sos.close();
    }
}
